package com.yizisu.playerlibrary.impl.exoplayer

import android.content.Context
import android.media.AudioManager
import androidx.media.AudioAttributesCompat
import androidx.media.AudioFocusRequestCompat
import androidx.media.AudioManagerCompat
import com.google.android.exoplayer2.MediaItem
import com.google.android.exoplayer2.SimpleExoPlayer
import com.google.android.exoplayer2.source.MediaSource

open class ExoPlayerConfig {
    /**
     * 创建exoPlayer播放器
     */
    open fun onCreateExoPlayer(context: Context): SimpleExoPlayer {
        return SimpleExoPlayer
            .Builder(context)
            .build()
    }

    /**
     * 获取没秒的时间间隔
     */
    open fun getTickPeriod(): Long {
        return 1000L
    }

    /**
     * 是否运行在子线程
     */
    open fun getTickOnThread(): Boolean {
        return false
    }

    /**
     * 创建音频焦点的请求类型
     */
    open fun onCreateAudioFocusRequestCompat(listener: AudioManager.OnAudioFocusChangeListener): AudioFocusRequestCompat {
        return AudioFocusRequestCompat.Builder(AudioManagerCompat.AUDIOFOCUS_GAIN_TRANSIENT)
            .setAudioAttributes(
                AudioAttributesCompat
                    .Builder()
                    .setContentType(AudioAttributesCompat.CONTENT_TYPE_MUSIC)
                    .setUsage(AudioAttributesCompat.USAGE_MEDIA)
                    .build()
            )
            .setOnAudioFocusChangeListener(listener)
            .build()
    }

    /**
     * 构建播放的媒体资源
     */
    open fun onBuildMediaSource(
        mediaItem: MediaItem,
        overrideExtension: String?
    ): MediaSource {
        return buildMediaSource(mediaItem, null, overrideExtension)
    }
}